package com.gexiaobao.pigeon.ui.race

import android.view.View
import androidx.databinding.ObservableInt
import androidx.lifecycle.MutableLiveData
import com.gexiaobao.pigeon.app.model.bean.CarMapRouteResponse
import com.gexiaobao.pigeon.app.model.bean.GatherListResponse
import com.gexiaobao.pigeon.app.model.bean.GroupResultResponse
import com.gexiaobao.pigeon.app.model.bean.GroupSignResponse
import com.gexiaobao.pigeon.app.model.bean.InsertGroupResponse
import com.gexiaobao.pigeon.app.model.bean.MemberCodeResponse
import com.gexiaobao.pigeon.app.model.bean.PassListResponse
import com.gexiaobao.pigeon.app.model.bean.RaceDetailInfoResponse
import com.gexiaobao.pigeon.app.model.bean.RaceTypeResponse
import com.gexiaobao.pigeon.app.model.bean.TraPassListResponse
import com.gexiaobao.pigeon.app.model.bean.UiState
import com.gexiaobao.pigeon.app.model.bean.VisitMemberListResponse
import com.gexiaobao.pigeon.app.model.param.ProjectListParams
import com.gexiaobao.pigeon.app.network.ListDataUiState
import com.gexiaobao.pigeon.app.network.apiService
import com.gexiaobao.pigeon.app.util.RxToast
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.callback.databind.BooleanObservableField
import me.hgj.jetpackmvvm.callback.databind.StringObservableField
import me.hgj.jetpackmvvm.ext.request

class RaceViewModel : BaseViewModel() {

    private var pageSize = 20
    private var pageNo = 1

    val gatherPigeonsTime = StringObservableField()//集鸽时间
    val gatherPigeonsAddress = StringObservableField()//集鸽地点
    val gatherPigeonsMobile = StringObservableField()//集鸽电话
    val gatherPigeonsFLyTime = StringObservableField()//飞行时间
    val gatherPigeonsSpeed = StringObservableField()//参考分速
    val validPigeonsSpeed = StringObservableField()//有效分速
    val trainingPigeonsTime = StringObservableField()//司放时间
    val trainingPigeonsAddress = StringObservableField()//司放地点
    val refereeZ = StringObservableField()//裁判长
    val refereeY = StringObservableField()//裁判员
    val pilot = StringObservableField()//放飞员
    val signUpFeatherNum = StringObservableField()//报名羽数
    val intoCageFeatherNum = StringObservableField()//归笼羽数
    val homingNum = StringObservableField()//归巢羽数
    val trainingWeather = StringObservableField()//放飞天气
    val airTemperature = StringObservableField()//气温
    val windPower = StringObservableField()//风向风力
    val gap = StringObservableField()//空距
    val longitude = StringObservableField()//经度
    val latitude = StringObservableField()//纬度

    val flyingState = BooleanObservableField()

    //综合排名
    var etSearchContent = StringObservableField()
    var totalPage = StringObservableField()

    //比赛结果
    var raceResultPageNo = StringObservableField()
    var percentage = StringObservableField()
    var signUpPigeonNum = StringObservableField()
    var homingPigeonNum = StringObservableField()
    var etSearchResult = StringObservableField()

    //放飞路线规划
    var passName = StringObservableField()
    var travelledDistance = StringObservableField()
    var currentLocation = StringObservableField()
    var carNo = StringObservableField()
    var startPoint = StringObservableField()
    var flyDistance = StringObservableField()

    //比赛结果
    val title = StringObservableField()

    //综合排名
    val rankingTitle = StringObservableField()

    /**  比赛关卡列表  */
    var racePassList: MutableLiveData<ListDataUiState<PassListResponse.DataList>> = MutableLiveData()

    /**  比赛详细信息  */
    var detailInfoResult: MutableLiveData<RaceDetailInfoResponse> = MutableLiveData()

    /**  插组-获取项目列表  */
    var groupResult: MutableLiveData<InsertGroupResponse> = MutableLiveData()

    /**  插组-报名  */
    var groupSignResult: MutableLiveData<GroupSignResponse> = MutableLiveData()

    /**  插组-成绩  */
    var groupScoreResult: MutableLiveData<GroupResultResponse> = MutableLiveData()

    /**  获取放飞车辆路线  */
    var carMapResult: MutableLiveData<CarMapRouteResponse> = MutableLiveData()

    /**  获取棚号  */
    var memberNumResult: MutableLiveData<MemberCodeResponse> = MutableLiveData()

    /**  综合排名  */
//    var totalRankResult: MutableLiveData<RaceRankingResponse> = MutableLiveData()
    var totalRankResult: MutableLiveData<Any> = MutableLiveData()

    /**  比赛结果  */
//    var raceResultResult: MutableLiveData<RaceResultResponse> = MutableLiveData()
    var raceResultResult: MutableLiveData<Any> = MutableLiveData()

    /** 修改密码 */
    var appointRaceResult: MutableLiveData<ListDataUiState<RaceTypeResponse.RaceTypeListBean>> = MutableLiveData()

    /** 训放赛回调结果 */
    var traPassResult: MutableLiveData<ListDataUiState<TraPassListResponse.DataList>> = MutableLiveData()

    /** 集鸽列表信息 */
    val gatherListResult: MutableLiveData<ListDataUiState<GatherListResponse.DataList>> = MutableLiveData()


    var isFlying = object : ObservableInt(flyingState) {
        override fun get(): Int {
            return if (flyingState.get()) {
                View.VISIBLE
            } else {
                View.GONE
            }
        }
    }

    /** 获取单用户下探视棚号列表  */
    val visitMemberResult: MutableLiveData<VisitMemberListResponse> = MutableLiveData()
    fun getVisitMemberNoList(raceId: String) {
        request({ apiService.getVisitMemberNoList(raceId) }, {
            visitMemberResult.value = it
        }, {
            RxToast.warning(it.msg)
        })
    }


    fun getPassListApp(raceId: String) {
        request({ apiService.getPassListApp(raceId) }, {
            val listDataUiState = ListDataUiState(
                isSuccess = true, isRefresh = true, isEmpty = it.isEmpty(), isFirstEmpty = it.isEmpty(), listData = it.list
            )
            racePassList.value = listDataUiState
        }, {
            val listDataUiState =
                ListDataUiState(isSuccess = false, isRefresh = true, errMessage = it.msg, listData = arrayListOf<PassListResponse.DataList>())
            racePassList.value = listDataUiState
        })
    }

    fun getProjectList(body: ProjectListParams) {
        request({ apiService.getProjectList(body) }, {
            val listDataUiState = ListDataUiState(
                isSuccess = true, isRefresh = true, isEmpty = it.isEmpty(), isFirstEmpty = it.isEmpty(), listData = it.list
            )
            appointRaceResult.value = listDataUiState
        }, {
            val listDataUiState =
                ListDataUiState(isSuccess = false, isRefresh = true, errMessage = it.msg, listData = arrayListOf<RaceTypeResponse.RaceTypeListBean>())
            appointRaceResult.value = listDataUiState
        })
    }

    //训放赛列表
    fun getTraPassList(raceId: Int) {
        request({ apiService.getTraPassList(raceId) }, {
            val listDataUiState = ListDataUiState(
                isSuccess = true, isRefresh = true, isEmpty = it.isEmpty(), isFirstEmpty = it.isEmpty(), listData = it.data
            )
            traPassResult.value = listDataUiState
        }, {
            val listDataUiState =
                ListDataUiState(isSuccess = false, isRefresh = true, errMessage = it.msg, listData = arrayListOf<TraPassListResponse.DataList>())
            traPassResult.value = listDataUiState
        })
    }

    fun getPassDetailApp(id: String) {
        request({ apiService.getPassDetailApp(id) }, {
            detailInfoResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    /** 插组-获取项目列表 */
    fun getGroupProject(raceId: String, racePassId: String, projectCode: String) {
        request({ apiService.getGroupProject(raceId, racePassId, projectCode, "1", "99999") }, {
            groupResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    /** 插组-查看报名 */
    fun getSignList(projectId: String, ringId: String, memberNum: String) {
        request({ apiService.getSignList(projectId, ringId, memberNum, "99999", "1") }, {
            groupSignResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    /** 插组-查看成绩 */
    fun getProjectResult(projectId: String, ringId: String, memberNum: String) {
        request({ apiService.getProjectResult(projectId, ringId, memberNum, "99999", "1") }, {
            groupScoreResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    //获取车辆放飞路线
    fun getCarMap(id: String) {
        request({ apiService.getCarMap(id) }, {
            carMapResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    //获取综合成绩
    fun getRaceResult(
        pageSize: String,
        pageNo: String,
        raceId: String,
        memberNum: String,
        ringId: String,
        fosterName: String,
        qStr: String
    ) {
        request({ apiService.getRaceResult(pageSize, pageNo, raceId, memberNum, ringId, fosterName, qStr) }, {
            totalRankResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    //获取棚号
    fun getMemberNum(id: String) {
        request({ apiService.getMemberNum(id) }, {
            memberNumResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    //根据比赛id获取棚号
    fun getRaceMemberNum(id: String) {
        request({ apiService.getRaceMemberNum(id) }, {
            memberNumResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    /** 比赛结果 */
    fun getPassResultWeb(
        passId: String,
        pageSize: String,
        pageNo: String,
        sortType: String,
        memberNum: String,
        ringId: String,
        fosterName: String,
        qStr: String
    ) {
        request({ apiService.passResultWeb(passId, pageSize, pageNo, sortType, memberNum, ringId, fosterName, qStr) }, {
            raceResultResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    /** 训放赛报名详情列表信息 */
    val gatherDetailResponse: MutableLiveData<GatherListResponse> = MutableLiveData()
    fun getGatherListForHand(passId: String, qStr: String, pageNo: Int) {
        request({ apiService.getGatherListForHand(passId, qStr, "", 50, pageNo) }, {
            gatherDetailResponse.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }
}